package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;


/**
 * 通过搜索添加好友，接受好友
 */
public class AcceptSearchFriendTaskProcess extends SetTaskExtra {
    private static AcceptSearchFriendTaskProcess ourInstance = new AcceptSearchFriendTaskProcess();

    public static AcceptSearchFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private AcceptSearchFriendTaskProcess() {
    }

    public static final int LOOK_UP_MAX_ITEM_NUM = 50;

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        Object needChatNickName = getExtra(WxUiViewConfig.KEY_NEED_CHAT_NICK_NAME);
        if (null == needChatNickName) {
            taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有昵称");
            return false;
        }

        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inContacts = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_CONTACTS);
            LogManager.getInstance().logInfo("inContacts = " + inContacts);
            if (inContacts) {
//                WxUiViewConfig.printViewGroup((ViewGroup) v);
                View newItem = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.ID_ACCEPT_FRIEND_LIST_ITEM, needChatNickName.toString());
                if (null != newItem) {
                    ViewOperation.clickView(newItem);
                } else {
                    View newFriend = v.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM);
                    if (null != newFriend) {
                        ViewOperation.clickView(newFriend);
                    }
                }
            }
        } else if (WxUiViewConfig.VIEW_NEW_FRIEND_LIST.equals(contextName)) {
//            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View list = v.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_VIEW);
            if (null != list && list instanceof ListView) {
                ListView lv = (ListView) list;
                for (int i = 0, len = lv.getCount(); i < len; i++) {
                    View childView = lv.getChildAt(i);
                    View nickName = WxUiViewConfig.getViewByIdAndContent((ViewGroup) childView, WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM_NICK_NAME, needChatNickName.toString());
                    if (null != nickName) {
                        View haveAdd = childView.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM_HAVE_ADD);
                        if (null != haveAdd && haveAdd.getVisibility() == View.VISIBLE) {
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "已经是好友了");
                            return true;
                        }
                        LogManager.getInstance().logInfo("找到对应的item");
                        View accept = childView.findViewById(WxUiViewConfig.ID_NEW_FRIEND_LIST_ITEM_ACCEPT);
                        if (null != accept && accept.getVisibility() == View.VISIBLE) {
                            ViewOperation.clickView(accept);
//                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "接受好友成功");
                            LogManager.getInstance().logInfo("接受好友成功");
                            return true;
                        }
                    }
                }

                int lastVisual = lv.getLastVisiblePosition();
                //执行到这里说明没有找到item
                if (lastVisual < LOOK_UP_MAX_ITEM_NUM) {
                    if (lastVisual + lv.getFooterViewsCount() + lv.getHeaderViewsCount() >= lv.getCount()) {
                        LogManager.getInstance().logInfo("已经移动到末尾了");
                        taskError(v.getContext(), WxTaskManger.TASK_SATE_FIND_FRIEND_ERROR, "没有找到好友请求");
                    } else {
                        LogManager.getInstance().logInfo("本页没有找到 移动到下一页 ");
                        ViewOperation.moveListPosition(lv, lastVisual);
                    }
                    LogManager.getInstance().logInfo("firstVisual = " + lv.getFirstVisiblePosition() + " lastVisual = " + lastVisual + " headCount = " + lv.getHeaderViewsCount() + " " + lv.getAdapter().getCount() + "  " + lv.getCount() + " " + lv.getFooterViewsCount());
                }

            }
        } else if (WxUiViewConfig.VIEW_CONTACT_INFO_UI.equals(contextName)) {
            View sendMessage = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_SEND);
            if (null != sendMessage && sendMessage.getVisibility() == View.VISIBLE) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "添加好友成功");
            }
        }
        return false;
    }
}
